﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Training.Library.Interfaces;
using Training.Library.DomainObject.Administration.Catalog;
using System.Data.SqlClient;
using System.Data;
using Training.Library.Enum;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace Training.Library.DataAccess.Administration.Catalog
{
    public partial class PesertaRepo : ISearchable<Peserta>
    {
        public List<Peserta> GetList(int pageSize, int currentPage, string sortName, string sortOrder,string parameters)
        {
            List<Peserta> tt = new List<Peserta>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      Peserta.PesertaID,Peserta.NIK, Peserta.NamaPeserta, Peserta.BagianID, Peserta.PositionID, Peserta.CreatedDate, Peserta.CreatedBy, Peserta.UpdatedDate, Peserta.UpdatedBy,Bagian.NamaBagian,Department.DepartmentDesc,Position.PositionName ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.Peserta INNER JOIN DBJobsDesc.master.Position ON Peserta.PositionID = Position.PositionID INNER JOIN DBJobsDesc.master.Department ON Position.DepartmentID = Department.DeptID INNER JOIN master.Bagian ON Peserta.BagianID=Bagian.BagianID ");
                    sbQuery.AppendFormat("      {0} ",parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                Peserta t = new Peserta(true);
                                t.PesertaID = dr[Peserta.Fields.PesertaID] == DBNull.Value ? 0 : int.Parse(dr[Peserta.Fields.PesertaID].ToString());
                                t.NIK = dr[Peserta.Fields.NIK] == DBNull.Value ? string.Empty : dr[Peserta.Fields.NIK].ToString();
                                t.NamaPeserta = dr[Peserta.Fields.NamaPeserta] == DBNull.Value ? string.Empty : dr[Peserta.Fields.NamaPeserta].ToString();
                                t.BagianID = dr[Peserta.Fields.BagianID] == DBNull.Value ? (int?)null : int.Parse(dr[Peserta.Fields.BagianID].ToString());
                                t.PositionID = dr[Peserta.Fields.PositionID] == DBNull.Value ? (int?)null : int.Parse(dr[Peserta.Fields.PositionID].ToString());
                                t.CreatedDate = dr[Peserta.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Peserta.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[Peserta.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[Peserta.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[Peserta.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Peserta.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[Peserta.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[Peserta.Fields.UpdatedBy].ToString();

                                t.Bagian = new Bagian(true);
                                t.Bagian.NamaBagian = dr[Bagian.Fields.NamaBagian] == DBNull.Value ? string.Empty : dr[Bagian.Fields.NamaBagian].ToString();
                                t.Bagian.Department.DepartmentDesc = dr[Department.Fields.DepartmentDesc] == DBNull.Value ? string.Empty : dr[Department.Fields.DepartmentDesc].ToString();
                                t.Position.PositionName = dr[Position.Fields.PositionName] == DBNull.Value ? string.Empty : dr[Position.Fields.PositionName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }


        public List<Peserta> GetListByPelatihan(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<Peserta> tt = new List<Peserta>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      Peserta.PesertaID,Peserta.NIK, Peserta.NamaPeserta, Peserta.BagianID, Peserta.PositionID, Peserta.CreatedDate, Peserta.CreatedBy, Peserta.UpdatedDate, Peserta.UpdatedBy,Bagian.NamaBagian,Department.DepartmentDesc,Position.PositionName ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.Peserta INNER JOIN DBJobsDesc.master.Position ON Peserta.PositionID = Position.PositionID INNER JOIN DBJobsDesc.master.Department ON Position.DepartmentID = Department.DeptID INNER JOIN master.Bagian ON Peserta.BagianID=Bagian.BagianID INNER JOIN transactions.PelatihanDetail ON Peserta.PesertaID = PelatihanDetail.PesertaID ");
                    sbQuery.AppendFormat("      {0} ", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                Peserta t = new Peserta(true);
                                t.PesertaID = dr[Peserta.Fields.PesertaID] == DBNull.Value ? 0 : int.Parse(dr[Peserta.Fields.PesertaID].ToString());
                                t.NIK = dr[Peserta.Fields.NIK] == DBNull.Value ? string.Empty : dr[Peserta.Fields.NIK].ToString();
                                t.NamaPeserta = dr[Peserta.Fields.NamaPeserta] == DBNull.Value ? string.Empty : dr[Peserta.Fields.NamaPeserta].ToString();
                                t.BagianID = dr[Peserta.Fields.BagianID] == DBNull.Value ? (int?)null : int.Parse(dr[Peserta.Fields.BagianID].ToString());
                                t.PositionID = dr[Peserta.Fields.PositionID] == DBNull.Value ? (int?)null : int.Parse(dr[Peserta.Fields.PositionID].ToString());
                                t.CreatedDate = dr[Peserta.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Peserta.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[Peserta.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[Peserta.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[Peserta.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Peserta.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[Peserta.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[Peserta.Fields.UpdatedBy].ToString();

                                t.Bagian = new Bagian(true);
                                t.Bagian.NamaBagian = dr[Bagian.Fields.NamaBagian] == DBNull.Value ? string.Empty : dr[Bagian.Fields.NamaBagian].ToString();
                                t.Bagian.Department.DepartmentDesc = dr[Department.Fields.DepartmentDesc] == DBNull.Value ? string.Empty : dr[Department.Fields.DepartmentDesc].ToString();
                                t.Position.PositionName = dr[Position.Fields.PositionName] == DBNull.Value ? string.Empty : dr[Position.Fields.PositionName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                       
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.Peserta {1}", column,parameters);
                            break;
                       
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public object SelectScalarByPelatihan(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {

                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.Peserta INNER JOIN DBJobsDesc.master.Position ON Peserta.PositionID = Position.PositionID INNER JOIN DBJobsDesc.master.Department ON Position.DepartmentID = Department.DeptID INNER JOIN master.Bagian ON Peserta.BagianID=Bagian.BagianID INNER JOIN transactions.PelatihanDetail ON Peserta.PesertaID = PelatihanDetail.PesertaID  {1}", column, parameters);
                            break;

                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public DataTable GetDataTableReport()
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {

                    StringBuilder sbQuery = new StringBuilder();

                    //sbQuery.AppendLine("SELECT");
                    //sbQuery.AppendLine("ROW_NUMBER() OVER(ORDER BY Peserta.NamaPeserta ASC) AS No");
                    //sbQuery.AppendLine(",Peserta.NIK");
                    //sbQuery.AppendLine(", Peserta.NamaPeserta");
                    //sbQuery.AppendLine(",Bagian.NamaBagian Bagian");
                    //sbQuery.AppendLine(",Department.DepartmentDesc Departemen");
                    //sbQuery.AppendLine(",Position.PositionName Jabatan");
                    //sbQuery.AppendLine("FROM  master.Peserta ");
                    //sbQuery.AppendLine("INNER JOIN DBJobsDesc.master.Position ON Peserta.PositionID = Position.PositionID ");
                    //sbQuery.AppendLine("INNER JOIN DBJobsDesc.master.Department ON Position.DepartmentID = Department.DeptID");
                    //sbQuery.AppendLine("INNER JOIN master.Bagian ON Peserta.BagianID=Bagian.BagianID ORDER BY Peserta.NamaPeserta ASC ");

                    //cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "SP_Peserta_Report";
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }
    }
}
